﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace HRProject.Code.DAO
{
    public class BaseCondition
    {
        private string paramName;
        private object paramValue;
        private ParameterDirection direction;

        public ParameterDirection Direction
        {
            get { return direction; }
            set { direction = value; }
        }

        public object ParamValue
        {
            get 
            {
                if (paramValue == null)
                    return DBNull.Value;
                return this.paramValue; 
            }
            set 
            {
                if (value == null)
                    this.paramValue = DBNull.Value;
                else
                    this.paramValue = value; 
            }
        }

        public string ParamName
        {
            get { return paramName; }
            set { paramName = value; }
        }

        public SqlDbType DbType
        {
            get
            {
                if (typeof(string).Equals(this.paramValue.GetType()))
                    return SqlDbType.VarChar;
                if (typeof(int).Equals(this.paramValue.GetType()))
                    return SqlDbType.Int;
                if (typeof(string).Equals(this.paramValue.GetType()))
                    return SqlDbType.VarChar;
                if (typeof(DateTime).Equals(this.paramValue.GetType()))
                    return SqlDbType.DateTime;
                return SqlDbType.Binary;
            }
        }

        public BaseCondition(string paramName, object paramValue, ParameterDirection direction)
        {
            this.paramName = paramName;
            if (paramValue == null)
                this.paramValue = DBNull.Value;
            else if ((paramValue is DateTime) && (paramValue.Equals(DateTime.MaxValue) || paramValue.Equals(DateTime.MinValue)))
                this.paramValue = DBNull.Value;
            else
                this.paramValue = paramValue;
            this.direction = direction;
        }
    }
}
